package com.kuang.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

//自定义的UserRealm  extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了 => 授权doGetAuthorizationInfo");
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了 => 认证doGetAuthenticationInfo");

        /**用户认证*/
        //用户名 密码 数据库中取
        String username = "root";
        String password = "123456";

        // UsernamePasswordToken token = new UsernamePasswordToken();
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        if(!userToken.getUsername().equals(username)){
            return null; //如果用户名不存在，会抛出UnknownAccountException用户名不存在异常
        }

        //密码认证，shiro自动帮你做
        return new SimpleAuthenticationInfo("",password,"");
    }
}
